package com.xjx.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xjx.dao.PermissionDao;
import com.xjx.dao.RoleDao;
import com.xjx.dao.UserDao;
import com.xjx.pojo.Permission;
import com.xjx.pojo.Role;
import com.xjx.pojo.User;
import com.xjx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

/**
 * @Version v1.0
 * @ClassName:
 * @Description:
 * @Author:xjx
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    @Override
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);
        if (user==null){
            return null;
        }
        Integer userId = user.getId();
        //根据用户id查询对应的角色
        Set<Role> roles = roleDao.findByUserId(userId);
        if (roles!=null&& roles.size()>0){
            for (Role role : roles) {
                Integer roleId = role.getId();
                //根据角色id查询对应的权限
                Set<Permission> permissionSet = permissionDao.findByRoleId(roleId);
                if (permissionSet!=null&&permissionSet.size()>0){
                    role.setPermissions(permissionSet);//让角色关联权限
                }
            }
            user.setRoles(roles);//让用户关联角色
        }
        return user;
    }
}
